package com.example.distinguish_server.Utils.Html;

import com.example.distinguish_server.Utils.Base64Util;
import com.example.distinguish_server.Utils.fileUtil.FileUtil;
import com.example.distinguish_server.entity.prop.TokenResponse;


import java.io.IOException;
import java.net.URLEncoder;

public class ResponseUtil {
    private static final String GRANT_TYPE = "client_credentials";
    private static final String CLIENT_ID = "QphkM6jByHhTdjvmV66b6SyA";
    private static final String CLIENT_SECRET = "59jThYKDeKP8uDlwPjCQGsLobNEiKvxq";
    public static  String FILEURL = "D:\\plateDetection\\";
    public static TokenResponse getBudaiToken(){
        TokenResponse tokenResponse= null;
        try {
            String postData = "grant_type="+GRANT_TYPE+"&client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET;
            String postResponse = HttpRequestUtils.sendPostRequest("https://aip.baidubce.com/oauth/2.0/token", postData);
             tokenResponse = TokenResponse.fromJson(postResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tokenResponse;
    }

    public static String getBudaiData(String FileName, String token, String multi_detect, String multi_scale) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
        try {
            // 本地文件路径
            String filePath = FILEURL + FileName;
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);

            String imgParam = URLEncoder.encode(imgStr, "UTF-8");


            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = token;
            String param = "image=" + imgParam + "&access_token=" + accessToken + "&multi_detect=" + multi_detect +"&multi_scale="+ multi_scale;
            String result = HttpUtil.post(url, param);
            System.out.println("result： " + result);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
